package com.feisuanyz.service.impl;

import com.feisuanyz.dto.LogoQuery;
import com.feisuanyz.dto.RestResult;
import com.feisuanyz.entity.LogoManagement;
import com.feisuanyz.entity.OperationLog;
import com.feisuanyz.repository.LogoManagementRepository;
import com.feisuanyz.repository.OperationLogRepository;
import com.feisuanyz.service.LogoService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class LogoServiceImpl implements LogoService {

    @Autowired
    private LogoManagementRepository logoManagementRepository;

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public RestResult<Void> createLogo(LogoQuery logoQuery) {
        try {
            LogoManagement logoManagement = modelMapper.map(logoQuery, LogoManagement.class);
            logoManagement.setCreateTime(new Date());
            logoManagementRepository.save(logoManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setLogoId(logoManagement.getLogoId());
            operationLog.setOperationType(OperationLog.OperationType.CREATE);
            operationLog.setDetails("Created logo with ID: " + logoManagement.getLogoId());
            operationLog.setCreateBy(logoQuery.getCreateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to create logo", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> editLogo(LogoQuery logoQuery) {
        try {
            LogoManagement existingLogo = logoManagementRepository.findById(logoQuery.getLogoId())
                    .orElseThrow(() -> new RuntimeException("Logo not found"));

            if (logoQuery.getEventId() != null) {
                existingLogo.setEventId(logoQuery.getEventId());
            }
            if (logoQuery.getLogoUrl() != null) {
                existingLogo.setLogoUrl(logoQuery.getLogoUrl());
            }
            existingLogo.setUpdateBy(logoQuery.getUpdateBy());

            logoManagementRepository.save(existingLogo);

            OperationLog operationLog = new OperationLog();
            operationLog.setLogoId(existingLogo.getLogoId());
            operationLog.setOperationType(OperationLog.OperationType.EDIT);
            operationLog.setDetails("Updated logo with ID: " + existingLogo.getLogoId());
            operationLog.setCreateBy(logoQuery.getUpdateBy());
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to edit logo", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> deleteLogo(Integer logoId, Integer deleteBy) {
        try {
            LogoManagement logoManagement = logoManagementRepository.findById(logoId)
                    .orElseThrow(() -> new RuntimeException("Logo not found"));

            logoManagementRepository.delete(logoManagement);

            OperationLog operationLog = new OperationLog();
            operationLog.setLogoId(logoId);
            operationLog.setOperationType(OperationLog.OperationType.DELETE);
            operationLog.setDetails("Deleted logo with ID: " + logoId);
            operationLog.setCreateBy(deleteBy);
            operationLog.setCreateTime(new Date());

            operationLogRepository.save(operationLog);

            return new RestResult<>("000000", "调用成功", null);
        } catch (Exception e) {
            log.error("Failed to delete logo", e);
            return new RestResult<>("999999", "系统异常", null);
        }
    }
}